package com.evl.api.common.service.impl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import com.evl.api.common.service.IOpendIdService;
import com.google.common.collect.Lists;

/**
 * 
 * @author lilong
 *
 */
@Service
public class OpendIdServiceImpl implements IOpendIdService {
	
	/**
	 * 获取opendId
	 */
	@Override
	public String getOpendId(String jsCode) {
		CloseableHttpClient httpClient = HttpClients.createDefault();  
        CloseableHttpResponse response = null;  
        InputStream is = null;  
        String url = "https://api.weixin.qq.com/sns/jscode2session";  
        //封装请求参数  
        List<NameValuePair> params = Lists.newArrayList();  
       /* params.add(new BasicNameValuePair("appid", "wx129992c510e0e631")); 
        params.add(new BasicNameValuePair("secret", "b47274f8994461d297790b371d6ec2c5")); */
        
        params.add(new BasicNameValuePair("appid", "wx129992c510e0e631")); 
        params.add(new BasicNameValuePair("secret", "b47274f8994461d297790b371d6ec2c5")); 

        params.add(new BasicNameValuePair("grant_type", "authorization_code")); 
        params.add(new BasicNameValuePair("js_code", jsCode)); 
        String str = "";  
        String body = ""; 
        StringBuilder sb = new StringBuilder();
        try {  
            //转换为键值对  
            str = EntityUtils.toString(new UrlEncodedFormEntity(params, Consts.UTF_8));  
            //创建Get请求  
            HttpGet httpGet = new HttpGet(url+"?"+str);  
            //执行Get请求，  
            response = httpClient.execute(httpGet);  
            //得到响应体  
            HttpEntity entity = response.getEntity();  
            if(entity != null){  
                is = entity.getContent();  
                //转换为字节输入流  
                BufferedReader br = new BufferedReader(new InputStreamReader(is, Consts.UTF_8));  
                 
                while((body=br.readLine()) != null){  
                    sb.append(body);
                }  
            }  
        } catch (ParseException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        } finally{  
            //关闭输入流，释放资源  
            if(is != null){  
                try {  
                    is.close();  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
            }  
            //消耗实体内容  
            if(response != null){  
                try {  
                    response.close();  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
            }  
            //关闭相应 丢弃http连接  
            if(httpClient != null){  
                try {  
                    httpClient.close();  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
            }  
        }  
		return sb.toString();
	}
	
}
